package fr.upstlse.glre.client.administration.communiquer.action;

import java.util.List;

import com.extjs.gxt.ui.client.binder.DataListBinder;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelStringProvider;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.user.client.rpc.AsyncCallback;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.administration.communiquer.Communiquer;
import fr.upstlse.glre.client.constante.AdminConstante;
import fr.upstlse.glre.client.espacededie.commun.utilisateur.AbstractFilterAction;
import fr.upstlse.glre.client.utilitaires.ControlerUtil;
import fr.upstlse.glre.client.vo.user.EtudiantSearchCriteriaVOGWT;
import fr.upstlse.glre.client.vo.user.UserRoleVOGWT;
import fr.upstlse.glre.client.vo.user.UserSearchCriteriaVOGWT;
import fr.upstlse.glre.client.vo.user.UserVOGWT;
import fr.upstlse.glre.client.widgets.modelData.UserModelData;

public class AdminFilterAction extends AbstractFilterAction {

	@Override
	public void actionFactory(String pCritere, String pValeur) {
		String lValeur = pValeur.replaceAll("\\*", "%");
		if(pCritere.equals(AdminConstante.COMMUNIQUER_FILTER_CRITERE_LOGIN.getValue())) {
			actionLogin(lValeur);
		}
		else if(pCritere.equals(AdminConstante.COMMUNIQUER_FILTER_CRITERE_ROLE.getValue())) {
			actionRole(lValeur);
		}
		else if(pCritere.equals(AdminConstante.COMMUNIQUER_FILTER_CRITERE_NOM.getValue())) {
			actionNom(lValeur);
		}
		else {
			MessageBox.alert("Attention", "Le critère " + pCritere + " n'est pas supporté", null);
		}
		Info.display("Information", "Recherche avec le critère " + pCritere + " et la valeur " + lValeur, "");
	}
	
	private void actionLogin(String pLogin) {
		AsyncCallback<List<UserVOGWT>> lLoginAsyncCallback = new AsyncCallback<List<UserVOGWT>>() {

			public void onFailure(Throwable pCaught) {
				MessageBox.alert("Erreur findUser ", pCaught.getMessage(),
						null);
			}

			public void onSuccess(List<UserVOGWT> result) {
				updateStore(result);
			}
		};
		UserSearchCriteriaVOGWT lUserSearchCriteriaVOGWT = new UserSearchCriteriaVOGWT();
		lUserSearchCriteriaVOGWT.setLogin(pLogin);
		ControlerUtil.getUserControlerServiceAsyncInstance().findUser(lUserSearchCriteriaVOGWT, lLoginAsyncCallback);
	}
	
	private void actionRole(String pRole) {
		AsyncCallback<List<UserVOGWT>> lRoleAsyncCallback = new AsyncCallback<List<UserVOGWT>>() {

			public void onFailure(Throwable pCaught) {
				MessageBox.alert("Erreur findUser ", pCaught.getMessage(),
						null);
				
			}

			public void onSuccess(List<UserVOGWT> result) {
				updateStore(result);
			}
		};
		UserSearchCriteriaVOGWT lUserSearchCriteriaVOGWT = new UserSearchCriteriaVOGWT();
		lUserSearchCriteriaVOGWT.setRole(UserRoleVOGWT.getUserRoleVOGWTs().get(pRole));
		ControlerUtil.getUserControlerServiceAsyncInstance().findUser(lUserSearchCriteriaVOGWT, lRoleAsyncCallback);
	}
	
	private void actionNom(String pNom) {
		AsyncCallback<List<UserVOGWT>> lNomAsyncCallback = new AsyncCallback<List<UserVOGWT>>() {

			public void onFailure(Throwable pCaught) {
				MessageBox.alert("Erreur findUser ", pCaught.getMessage(),
						null);
			}

			public void onSuccess(List<UserVOGWT> result) {
				updateStore(result);
			}
		};
		EtudiantSearchCriteriaVOGWT lEtudiantSearchCriteriaVOGWT = new EtudiantSearchCriteriaVOGWT();
		lEtudiantSearchCriteriaVOGWT.setNom(pNom);
		ControlerUtil.getUserControlerServiceAsyncInstance().findEtudiantUser(lEtudiantSearchCriteriaVOGWT, lNomAsyncCallback);
	}
	
	private void updateStore(List<UserVOGWT> pUserVOGWTs) {
		final ListStore<UserModelData> lStore = new ListStore<UserModelData>();
		lStore.add(UserModelData.toUserModelDatas(pUserVOGWTs));
		
		DataListBinder<UserModelData> binder = new DataListBinder<UserModelData>(
				Communiquer.getInstance().getListeUserPanel(), lStore);
		binder.setDisplayProperty(Communiquer.getInstance().getBinder().getDisplayProperty());
		binder.setStringProvider(new ModelStringProvider<ModelData>() {

					public String getStringValue(ModelData model,
							String property) {
						String lTexte = (String) model
								.get(RfgConstante.USER_MODELDATA_LOGIN);
						lTexte += " ("
								+ (String) model
										.get(RfgConstante.USER_MODELDATA_ROLE)
								+ ")";
						return lTexte;
					}

				});
		binder.init();
	}

}
